package com.ruoyi.web.controller.system;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.UUID;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.system.domain.SysCusService1Bi;
import com.ruoyi.system.service.ISysCusService1BiService;
import io.swagger.models.auth.In;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.SysServiceWorkOrderBi;
import com.ruoyi.system.service.ISysServiceWorkOrderBiService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * SysServiceWorkOrderBiController
 * 
 * @author SYF
 * @date 2025-06-04
 */
@RestController
@RequestMapping("/system/SysServiceWorkOrderBi")
public class SysServiceWorkOrderBiController extends BaseController
{
    @Autowired
    private ISysServiceWorkOrderBiService sysServiceWorkOrderBiService;

    @Autowired
    private ISysCusService1BiService cusService1BiService;

    /**
     * 查询SysServiceWorkOrderBi列表
     */
    @GetMapping("/list")
    public TableDataInfo list(SysServiceWorkOrderBi sysServiceWorkOrderBi)
    {
        startPage();
        List<SysServiceWorkOrderBi> list = sysServiceWorkOrderBiService.selectSysServiceWorkOrderBiList(sysServiceWorkOrderBi);
        return getDataTable(list);
    }

    /**
     * 导出SysServiceWorkOrderBi列表
     */
    @Log(title = "SysServiceWorkOrderBi", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysServiceWorkOrderBi sysServiceWorkOrderBi)
    {
        List<SysServiceWorkOrderBi> list = sysServiceWorkOrderBiService.selectSysServiceWorkOrderBiList(sysServiceWorkOrderBi);
        ExcelUtil<SysServiceWorkOrderBi> util = new ExcelUtil<SysServiceWorkOrderBi>(SysServiceWorkOrderBi.class);
        util.exportExcel(response, list, "SysServiceWorkOrderBi数据");
    }

    /**
     * 获取SysServiceWorkOrderBi详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id) {
        return success(sysServiceWorkOrderBiService.selectSysServiceWorkOrderBiByYear(id));
    }

    /**
     * 新增SysServiceWorkOrderBi
     */
    @Log(title = "SysServiceWorkOrderBi", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysServiceWorkOrderBi order) {
        order.setWkDay(order.getWkYear());
        String uuid = UUID.randomUUID().toString().replaceAll("-","");
        order.setId(uuid);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date date = null;
        try {
            date = sdf.parse(order.getWkYear());
        }catch (ParseException e){
            e.printStackTrace();
        }
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        // 获取一年中的第几周，具体取决于 Calendar 类的设置
        int weekOfYear = calendar.get(Calendar.WEEK_OF_YEAR);
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.YEAR, Integer.parseInt(order.getYear()));
        cal.set(Calendar.WEEK_OF_YEAR, weekOfYear);
        cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
        String monday = sdf.format(cal.getTime());
        // 获取周一和周日的日期
        Calendar endOfWeek = Calendar.getInstance();
        endOfWeek.setTime(cal.getTime());
        endOfWeek.add(Calendar.DAY_OF_WEEK, 6);
        String sunday = sdf.format(endOfWeek.getTime());
        String wkYear = monday+"至"+sunday+"("+weekOfYear+"wk)";
        order.setWkYear(wkYear);
        sysServiceWorkOrderBiService.insertSysServiceWorkOrderBi(order);
        if(order.getIsFault().equalsIgnoreCase("1")){
            SysCusService1Bi cus = new SysCusService1Bi();
            cus.setOrderId(order.getId());
            cus.setQd(sdf.format(new Date()));
            cus.setShType("客服回访");
            cus.setZmpz(order.getBigName());
            cus.setArea(order.getArea());
            cus.setShengzZhouqu(order.getZhouqi());
            cus.setGid(order.getRemark());
            cus.setQty(1L);
            cus.setWkYear(wkYear);
            cus.setCus(order.getCus());
            cus.setSaleman(order.getSlaeman());
            cusService1BiService.insertSysCusService1Bi(cus);
        }
        return AjaxResult.success();
    }

    /**
     * 修改SysServiceWorkOrderBi
     */
    @Log(title = "SysServiceWorkOrderBi", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysServiceWorkOrderBi order) {
        order.setWkDay(order.getWkYear());
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date date = null;
        try {
            date = sdf.parse(order.getWkYear());
        }catch (ParseException e){
            e.printStackTrace();
        }
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        // 获取一年中的第几周，具体取决于 Calendar 类的设置
        int weekOfYear = calendar.get(Calendar.WEEK_OF_YEAR);

        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.YEAR, Integer.parseInt(order.getYear()));
        cal.set(Calendar.WEEK_OF_YEAR, weekOfYear);
        cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
        String monday = sdf.format(cal.getTime());
        // 获取周一和周日的日期
        Calendar endOfWeek = Calendar.getInstance();
        endOfWeek.setTime(cal.getTime());
        endOfWeek.add(Calendar.DAY_OF_WEEK, 6);
        String sunday = sdf.format(endOfWeek.getTime());
        String wkYear = monday+"至"+sunday+"("+weekOfYear+"wk)";
        order.setWkYear(wkYear);
        sysServiceWorkOrderBiService.updateSysServiceWorkOrderBi(order);
        // 1.删除
        cusService1BiService.deleteCusService1ByOrderId(order.getId());
        // 2.后检查插入
        if(order.getIsFault().equalsIgnoreCase("1")){
            SysCusService1Bi cus = new SysCusService1Bi();
            cus.setOrderId(order.getId());
            cus.setQd(sdf.format(new Date()));
            cus.setShType("客服回访");
            cus.setZmpz(order.getBigName());
            cus.setArea(order.getArea());
            cus.setShengzZhouqu(order.getZhouqi());
            cus.setGid(order.getRemark());
            cus.setQty(1L);
            cus.setWkYear(wkYear);
            cus.setCus(order.getCus());
            cus.setSaleman(order.getSlaeman());
            cusService1BiService.insertSysCusService1Bi(cus);
        }
        return AjaxResult.success();
    }

    /**
     * 删除SysServiceWorkOrderBi
     */
    @Log(title = "SysServiceWorkOrderBi", businessType = BusinessType.DELETE)
	@DeleteMapping("/{years}")
    public AjaxResult remove(@PathVariable String[] years)
    {
        return toAjax(sysServiceWorkOrderBiService.deleteSysServiceWorkOrderBiByYears(years));
    }
}
